package service;

import java.util.LinkedHashMap;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;

import basic.PageInfo;
import basic.QuerySet;
import basic.ResultSet;
import common.SqlHandler;
import dao.IGlobalDao;
import interfaces.IGlobalService;
import utils.StringUtil;

@Service
public class GlobalService implements IGlobalService {

	@Autowired
	private IGlobalDao globalDao;

	@SuppressWarnings("rawtypes")
	public ResultSet<LinkedHashMap<String, Object>> search(QuerySet<Object> querySet) throws Exception {
		ResultSet<LinkedHashMap<String, Object>> resultSet = new ResultSet<>();
		if (StringUtil.isEmpty(querySet.getSqlInfo())) {
			return resultSet;
		}
		PageInfo pageInfo = querySet.getPageInfo();
		if (StringUtil.isEmpty(pageInfo)) {
			pageInfo = new PageInfo();
		}
		Page page = PageHelper.startPage(pageInfo.getPageIndex(), pageInfo.getPageSize(), true);
		resultSet.setResultList(globalDao.query(SqlHandler.filter(querySet.getSqlInfo())));
		pageInfo.setPageRecord(new Long(page.getTotal()).intValue());
		pageInfo.allocation();
		resultSet.setPageInfo(pageInfo);
		return resultSet;
	}

}
